package com.mazaiting.mail.listeners;

import com.mazaiting.common.core.domain.result.ResultCode;
import com.mazaiting.common.core.ex.exceptions.EmptyObjectException;
import com.mazaiting.email.constant.MailConstant;
import com.mazaiting.email.domain.info.MailDetail;
import com.mazaiting.email.service.IMailService;
import com.mazaiting.mail.domain.entity.MsgEmailSend;
import com.mazaiting.mail.service.IMsgEmailSendService;
import com.mazaiting.rabbitmq.executor.IMessageExecutor;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 邮件预警 MQ 监听
 * RabbitListener 可以自动创建队列, 交换机
 * 但需要将当前类交给 Spring 管理
 * @author mazaiting
 * @create_time 2022/3/28 19:23
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MailListener {

    private final IMessageExecutor messageExecutor;

    private final IMailService mailService;

    private final IMsgEmailSendService msgEmailSendService;

    /**
     * 消息监听
     * 监听时使用 DL 开始的前缀
     */
    @RabbitListener(
            containerFactory = "retryRabbitListenerContainerFactory",
            bindings = @QueueBinding(
                    value = @Queue(MailConstant.QUEUE_EMAIL_ALARM_DELAY),
                    exchange = @Exchange(value = MailConstant.EXCHANGE_EMAIL_ALARM_DELAY),
                    key = MailConstant.KEY_EMAIL_ALARM_DELAY
            )
    )
    public void receiveSms(Message message, Channel channel) {
        messageExecutor.execute(message, channel, Long.class, sendId -> {
            // 根据 ID 获取
            MsgEmailSend msgEmailSend = msgEmailSendService.getById(sendId);
            if (Objects.isNull(msgEmailSend)) {
                throw new EmptyObjectException(ResultCode.MSG_MAIL_SEND_EMPTY);
            }
            // 判断是否已经发送成功
            if (msgEmailSend.getStatus()) {
                throw new EmptyObjectException(ResultCode.MSG_MAIL_SEND_SUCCESS);
            }

            // 消息转换
            MailDetail mailDetail = new MailDetail();
            mailDetail.setFromMail(msgEmailSend.getFromMail());
            mailDetail.setToMail(msgEmailSend.getToMail());
            mailDetail.setTitle(msgEmailSend.getTitle());
            mailDetail.setContent(msgEmailSend.getContent());
            // 抄送人
            String cc = msgEmailSend.getCc();
            if (StringUtils.hasLength(cc)) {
                mailDetail.setCc(cc.split(","));
            }
            mailDetail.setHtml(true);
            // 附件
            List<String> attachments = msgEmailSend.getAttachments();
            if (!CollectionUtils.isEmpty(attachments)) {
                // 遍历文件 ID
                for (String fileId : attachments) {
//                    mailDetail.setAttachments();
                }
            }
            mailService.send(mailDetail);
        });
    }
}